using System;
using System.Web;
using System.Collections;
using System.Configuration;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.OleDb;
using System.Data;


/// <summary>
/// Summary description for RegistarNaImoti
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RegistarNaImoti : System.Web.Services.WebService {

    public RegistarNaImoti () {

        
    }

    /////////////////////////////////////////////////////////////
    ///////////Interfejs za web servisi/////////////////////////
    ////////////////////////////////////////////////////////////

    //Proveri pole - za dadeno pole proveruva koj e sopstvenikot - go vraka imeto na sopstvenikot, ako poleto nema sopstvenik funkcijata vraka null
    [WebMethod]
    public string proveriPole(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string select = "SELECT IGRACI.IME FROM IGRACI, POLINJA WHERE POLINJA.ID = ? AND POLINJA.IGRACID = IGRACI.ID";
        OleDbCommand select_pole = new OleDbCommand(select, conn);
        select_pole.Parameters.AddWithValue("?", id_pole);

        object ime_igrac = select_pole.ExecuteScalar();
        conn.Close();

        if (ime_igrac == null)
        {
            //return null;
            return "Poleto nema sopstvenik!";
        }
        else
        {
            return ime_igrac.ToString();
        }
        
    }

    //Proveri imot - za daden igrac proveruva koi polinja gi poseduva - gi vraka iminjata na polinjata, ako igracot nema polinja vraka null
    [WebMethod]
    public string[] proveriImot(int id_igrac)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string select = "SELECT IME FROM POLINJA WHERE IGRACID = ?";
        OleDbCommand select_pole = new OleDbCommand(select, conn);
        select_pole.Parameters.AddWithValue("?", id_igrac);

        OleDbDataReader reader = select_pole.ExecuteReader();

        
        int n = 0;
       
        while (reader.Read())
        {
             n++;
            
        }

        string []polinja = new string[n];
        reader.Close();

        OleDbDataReader reader2 = select_pole.ExecuteReader();

        if (reader2.HasRows)
        {
            int i = 0;
            while (reader2.Read())
            {
                polinja[i] = reader2.GetValue(0).ToString();
                i++;
            }
            reader2.Close();
            conn.Close();
            return polinja;
        }
        else
        {
            return null;
        }

    }

    //Proveri kuki - za dadeno pole proveruva kolku kuki ima izgradeno
    [WebMethod]
    public int proveriKuki(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string select = "SELECT KUKI FROM POLINJA WHERE ID = ?";
        OleDbCommand select_pole = new OleDbCommand(select, conn);
        select_pole.Parameters.AddWithValue("?", id_pole);

        int kuki = Int32.Parse(select_pole.ExecuteScalar().ToString());

        return kuki;

    }

    //Proveri hotel - za dadeno pole proveruva kolku hoteli ima izgradeno
    [WebMethod]
    public int proveriHoteli(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string select = "SELECT HOTEL FROM POLINJA WHERE ID = ?";
        OleDbCommand select_pole = new OleDbCommand(select, conn);
        select_pole.Parameters.AddWithValue("?", id_pole);

        int hotel = Int32.Parse(select_pole.ExecuteScalar().ToString());

        return hotel;
    }

    //Dodeluvanje pole na nekoj igrac (kupuvanje imot), funkcijata se povikuva so imeto na igracot i imeto na poleto, ako dodeluvanjeto e uspesno vraka true inaku false
    [WebMethod]
    public string dodeliPole(int id_igrac, int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        //string select = "SELECT ID FROM IGRACI WHERE IME = ?";//ne radi
        //OleDbCommand select_igraci = new OleDbCommand(select, conn);
        //select_igraci.Parameters.AddWithValue("?", ime_igrac);
        //update_polinja.Parameters.AddWithValue("?", ime_pole);
        //OleDbDataReader reader = select_igraci.ExecuteReader();

        //reader.Read();
        //string id = reader.GetValue(0).ToString();

        string update = "UPDATE POLINJA SET IGRACID = ? WHERE ID = ? AND IGRACID IS NULL";
        OleDbCommand update_pole = new OleDbCommand(update, conn);
        update_pole.Parameters.AddWithValue("?", id_igrac);
        update_pole.Parameters.AddWithValue("?", id_pole);


        if (update_pole.ExecuteNonQuery() > 0)
        {
            //reader.Close();
            conn.Close();
            //return true;
            return "Kupuvanjeto na poleto e uspesno!";
        }
        else
        {
            //reader.Close();
            conn.Close();
            //return false;
            return "Poleto nemoze da se kupi!";
        }
        
    }

    //Prodazba na pole, ovaa funkcija za dadeno pole go brise negoviot sopstvenik
    //Funkcijata raboti vo sklad so pravilata na igrata monopol pa taka edno pole nemoze da se prodade ako
    //na nekoe od polinjata od istata grupa (boja) ima izgradeno kuka ili hotel
    //poleto moze da se prodade samo ako site polinja od taa grupa se prazni
    //ako prodazbata e uspesna funkcijata vraka vrednost true inaku false
    [WebMethod]
    public string prodadiPole(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        string select = "SELECT SUM(KUKI) FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?)";
        OleDbCommand select_kuki = new OleDbCommand(select, conn);
        select_kuki.Parameters.AddWithValue("?", id_pole);
        int kuki = Int32.Parse(select_kuki.ExecuteScalar().ToString());

        string select2 = "SELECT SUM(HOTEL) FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?)";
        OleDbCommand select_hotel = new OleDbCommand(select2, conn);
        select_hotel.Parameters.AddWithValue("?", id_pole);
        int hoteli = Int32.Parse(select_hotel.ExecuteScalar().ToString());

        if (kuki == 0 && hoteli == 0)
        {
            string update = "UPDATE POLINJA SET IGRACID = NULL WHERE ID = ?";
            OleDbCommand update_pole = new OleDbCommand(update, conn);
            update_pole.Parameters.AddWithValue("?", id_pole);
            update_pole.ExecuteNonQuery();
            //return true;
            return "Prodazbata na poleto e uspesna!";
        }
        else
        {
            //return false;
            return "Poleto nemoze da se prodade!";
        }
    }

    //Gradenje kuka na dadeno pole, funkcijata se povikuva so imeto na poleto na koe se gradi, ovaa funkcija gi sledi pravilata na gradenje
    //na kuka vo igrata monopol odnosno ke izgradi kuka na poleto ako se gradi ramnomerno na site polinja od edna grupa - nema da dozvoli
    //na edno pole od edna boja da ima 2 a na drugo nitua edna kuka.
    //Isto taka igracot mora da gi poseduva site polinja od edna boja za da gradi kuki na nekoe od niv
    [WebMethod]
    public string izgradiKuka(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        string select = "SELECT KUKI FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?)";
        OleDbCommand select_kuki = new OleDbCommand(select, conn);
        select_kuki.Parameters.AddWithValue("?", id_pole);
        OleDbDataReader reader = select_kuki.ExecuteReader();

        string select2 = "SELECT KUKI FROM POLINJA WHERE ID = ?";
        OleDbCommand select_kuki2 = new OleDbCommand(select2, conn);
        select_kuki2.Parameters.AddWithValue("?", id_pole);
        int kuki = Int32.Parse(select_kuki2.ExecuteScalar().ToString());

        string select3 = "SELECT COUNT(ID) FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?)";
        OleDbCommand broj_polinja = new OleDbCommand(select3, conn);
        broj_polinja.Parameters.AddWithValue("?", id_pole);
        int count = Int32.Parse(broj_polinja.ExecuteScalar().ToString());

        string select4 = "SELECT COUNT(ID) FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?) AND IGRACID = (SELECT IGRACID FROM POLINJA WHERE ID = ?) AND IGRACID IS NOT NULL";
        OleDbCommand broj_polinja2 = new OleDbCommand(select4, conn);
        broj_polinja2.Parameters.AddWithValue("?", id_pole);
        broj_polinja2.Parameters.AddWithValue("?", id_pole);
        int count2 = Int32.Parse(broj_polinja2.ExecuteScalar().ToString());

        if (count != count2)
        {
            //return false;
            return "Igracot ne gi poseduva site polinja od dadenata gurpa!";
        }

        while (reader.Read())
        {
            int br = Int32.Parse(reader.GetValue(0).ToString());
            if ((kuki + 1) - br > 1 || (kuki + 1) > 4)
            {
                //return false;
                return "Mora da gradite ramnomerno!";
            }
        }

        string update = "UPDATE POLINJA SET KUKI = ? WHERE ID = ?";
        OleDbCommand update_kuki = new OleDbCommand(update, conn);
        update_kuki.Parameters.AddWithValue("?", (kuki+1));
        update_kuki.Parameters.AddWithValue("?", id_pole);

        if (update_kuki.ExecuteNonQuery() > 0)
        {
            //return true;
            return "Gradenjeto e uspesno izvrseno!";
        }
        else
        {
            //return false;
            return "Greska vo bazata!";
        }
    }

    //Gradenje hotel na dadeno pole
    //Ovaa funkcija se povikuva so imeto na poleto kade treba da se gradi hotel
    //Funkcijata ke izgradi hotel na poleto samo ako na site polinja od taa grupa ima po 4 kuki
    //Otkako ke se igradi hotelot, spored pravilata na igrata se brisat site kuki na toa pole
    [WebMethod]
    public string izgradiHotel(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        string select = "SELECT KUKI FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?) AND HOTEL = 0";//ne radi
        OleDbCommand select_kuki = new OleDbCommand(select, conn);
        select_kuki.Parameters.AddWithValue("?", id_pole);
        OleDbDataReader reader = select_kuki.ExecuteReader();

        while (reader.Read())
        {
            if (Int32.Parse(reader.GetValue(0).ToString()) != 4)
            {
                //return false;
                return "Nema dovolno izgradeni kuki na ova pole ili na nekoe drugo od istata grupa za da moze da se gradi hotel!";
            }
        }

        string delete = "UPDATE POLINJA SET KUKI = 0 WHERE ID = ?";
        OleDbCommand delete_kuki = new OleDbCommand(delete, conn);
        delete_kuki.Parameters.AddWithValue("?", id_pole);
        delete_kuki.ExecuteNonQuery();

        string update = "UPDATE POLINJA SET HOTEL = 1 WHERE ID = ?";
        OleDbCommand update_hotel = new OleDbCommand(update, conn);
        update_hotel.Parameters.AddWithValue("?", id_pole);

        if (update_hotel.ExecuteNonQuery() > 0)
        {
            //return true;
            return "Gradenjeto e uspesno izvrseno!";
        }
        else
        {
            //return false;
            return "Greska vo bazata!";
        }
    }

    //brisenje (prodazba) na hotel
    //Go otstranuva hotelot od poleto
    [WebMethod]
    public string prodadiHotel(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string delete = "UPDATE POLINJA SET HOTEL = 0 WHERE ID = ?";
        OleDbCommand delete_hotel = new OleDbCommand(delete, conn);
        delete_hotel.Parameters.AddWithValue("?", id_pole);

        if (delete_hotel.ExecuteNonQuery() > 0)
        {
            //return true;
            return "Prodazbata e uspesno izvrsena!";
        }
        else
        {
            //return false;
            return "Greska vo bazata!";
        }
    }

    //Ovaa funkcija se povikuva so imeto na poleto od kade se brise kukata
    //Funkcijata ke ja izbrise kukata samo ako brisenjeto se vrsi ramnomerno, sto znaci deka nema da dozvoli da se izbrise
    //kuka na pole koe ima 2 kuki ako site ostanati imaat po 3 kuki
    [WebMethod]
    public string prodadiKuka(int id_pole)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();

        string select = "SELECT KUKI FROM POLINJA WHERE BOJA = (SELECT BOJA FROM POLINJA WHERE ID = ?)";//ne radi
        OleDbCommand select_kuki = new OleDbCommand(select, conn);
        select_kuki.Parameters.AddWithValue("?", id_pole);
        OleDbDataReader reader = select_kuki.ExecuteReader();

        string select2 = "SELECT KUKI FROM POLINJA WHERE ID = ?";
        OleDbCommand select_kuki2 = new OleDbCommand(select2, conn);
        select_kuki2.Parameters.AddWithValue("?", id_pole);
        int kuki = Int32.Parse(select_kuki2.ExecuteScalar().ToString());

        if (kuki == 0)
        {
            //return false;
            return "Na poleto nema izgradeno kuki koi moze da se prodadat!";
        }

        while (reader.Read())
        {
            int br = Int32.Parse(reader.GetValue(0).ToString());

            if (br - (kuki - 1) > 1)
            {
                //return false;
                return "Mora da podavate ramnomerno!";
            }
        }

        string update = "UPDATE POLINJA SET KUKI = ? WHERE ID = ?";
        OleDbCommand update_kuki = new OleDbCommand(update, conn);
        update_kuki.Parameters.AddWithValue("?", (kuki - 1));
        update_kuki.Parameters.AddWithValue("?", id_pole);

        if (update_kuki.ExecuteNonQuery() > 0)
        {
            //return true;
            return "Prodazbata e uspesno izvrsena!";
        }
        else
        {
            //return false;
            return "Greska vo bazata!";
        }
    }

    //inicijalizacija na bazata so igraci koi se momentalno vo igra
    //Ovaa funkcija treba da se povika na pocetok na sekoja igra od strana na kolegite koi gi soedinuvaat
    //proektite
    //Funkcijata ja polni bazata so igraci i dokolku istata ne se povika nitu eden od gorenavedenite metodi nema da moze da 
    //ja izvrsuva svojata zadaca
    [WebMethod]
    public string vnesiIgraci(string[] igraci)
    {
        string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/Proekt.mdb;"; //postavuvame "adresa" do bazata
        OleDbConnection conn = new OleDbConnection(conString);
        conn.Open();

        string select = "SELECT ID FROM IGRACI";
        OleDbCommand select_igraci = new OleDbCommand(select, conn);
        OleDbDataReader reader = select_igraci.ExecuteReader();

        if (reader.HasRows)
        {
            //return false;
            return "Vo bazata veke postojat igraci!";
        }
        else
        {
            int i;
            for (i = 0; i < igraci.Length; i++)
            {
                string insert = "INSERT INTO IGRACI (IME) VALUES (?)";
                OleDbCommand insert_igraci = new OleDbCommand(insert, conn);

                string temp = igraci[i];
                insert_igraci.Parameters.AddWithValue("?", temp);
                insert_igraci.ExecuteNonQuery();
            }
            //return true;
            return "Bazata e uspesno napolneta so igraci!";
        }
    }

}

